package gui.main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import gui.DBConnect;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


public class Naplo {
	private static JPanel naplo;

	
	public static JPanel createNaplo(){
		naplo = new JPanel();
		naplo.setLayout(new BorderLayout());

		
	    DBConnect dbc = new DBConnect();
	    Vector<dataTypes.Naplo> sorok = dbc.NaploListaz();

	    String[] columns = { "ID", "Usernév", "Dátum", "Tevékenység" };
	    Object[][] rows = convertArray(sorok);


	   TableModel model = new DefaultTableModel(rows, columns);
	    JTable table = new JTable(model);
	    table.setDragEnabled(false);
	    table.setEnabled(false);
	    final TableRowSorter<TableModel> sorter;
	    sorter = new TableRowSorter<TableModel>(model);
	    table.setRowSorter(sorter);
	    
	    JPanel keresopanel = new JPanel();
	    keresopanel.add(new JLabel("Szűrés:"));
	    final JTextField kereses = new JTextField(25);
	    kereses.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e) {
	        	  String expr = kereses.getText();  
	        	  sorter.setRowFilter(RowFilter.regexFilter(expr));
	              sorter.setSortKeys(null);
	         }
	    }    
	    );
	 
	   
	    keresopanel.add(kereses);
	    naplo.add(keresopanel,BorderLayout.PAGE_END);
	    naplo.add(new JScrollPane(table),BorderLayout.CENTER);
		
		
		
		return naplo;
	}
	
	public static Object[][] convertArray(Vector<dataTypes.Naplo> kat){
		Object[][] con = new Object[kat.size()][4];
		
		for(int i=0; i<kat.size(); i++){			
			con[i][0] = kat.elementAt(i).id;
			con[i][1] = kat.elementAt(i).kicsoda;
			con[i][2] = kat.elementAt(i).datum;
			con[i][3] = kat.elementAt(i).tevekenyseg;
		}
	
		return con;
	
	}

}
